package com.osmino.contacts.groups.edit;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.osmino.contacts.R;
import com.osmino.contacts.entity.Group;
import com.osmino.core.GroupEditItemController;
import com.osmino.core.style.IStyleableView;
import com.osmino.core.style.Style;
import com.osmino.core.utils.view.Backgroung;

public class GroupEditItem extends LinearLayout implements IStyleableView,
		OnClickListener {

	private final boolean DBG = true;

	private static final int GROUP_DELETE_IB = R.id.group_edit_item_delete;
	private static final int GROUP_NAME_TV = R.id.group_edit_item_name;//
	private static final int GROUP_ACCOUNT_NAME_TV = R.id.group_edit_item_account;//
	private static final int GROUP_PIC_IB = R.id.group_edit_item_image;//
	private static final int GROUP_RINGTONE_IB = R.id.group_edit_item_ringtone;
	private static final int GROUP_EDIT_IB = R.id.group_edit_item_edit;
	private static final int GROUP_LIST_IB = R.id.icon;

	private GroupEditItemController mController;
	private Group mInfo;

	public GroupEditItem(Context context, GroupEditItemController client) {
		super(context);
		this.mController = client;
		LayoutInflater.from(context).inflate(R.layout.group_list_edit_item,
				this);
//		prepCallbacks();
//		prepBgs();
	}

	private void prepBgs() {
		int[] buttons = new int[] { 
				GROUP_DELETE_IB, 
				GROUP_RINGTONE_IB,
				GROUP_EDIT_IB, 
				GROUP_LIST_IB };
		int[] buttonsIc = new int[] { 
				R.drawable.groups_edit_del_ic,
				R.drawable.groups_edit_ringtone_ic,
				R.drawable.groups_edit_edit_ic, 
				R.drawable.groups_edit_drag_ic};

		int[] buttonsIcAct = new int[] { 
				R.drawable.groups_edit_del_ic_act,
				R.drawable.groups_edit_ringtone_ic_act,
				R.drawable.groups_edit_edit_ic_act, 
				R.drawable.groups_edit_drag_ic_act};

		for (int i=0;i<buttons.length;i++) {
			ImageButton ib = (ImageButton) findViewById(buttons[i]);
			Backgroung.setResource(ib, buttonsIcAct[i], buttonsIc[i]);
		}
	}

	private void prepCallbacks() {
		int[] buttons = new int[] { GROUP_DELETE_IB, GROUP_PIC_IB,
				GROUP_RINGTONE_IB, GROUP_EDIT_IB, /**GROUP_LIST_IB **/};
		for (int i : buttons) {
			findViewById(i).setOnClickListener(this);
		}
	}

	public void clearInfo() {
		TextView tvName = (TextView) findViewById(GROUP_NAME_TV);
		TextView tvAccount = (TextView) findViewById(GROUP_ACCOUNT_NAME_TV);
		ImageView iv = (ImageView) findViewById(GROUP_PIC_IB);
		tvName.setText("");
		tvAccount.setText("");
		iv.setImageDrawable(null);
	}

	public void setGroup(Group group) {
		if(group==null)
			throw new NullPointerException();
		mInfo = group;
		clearInfo();
		TextView tvName = (TextView) findViewById(GROUP_NAME_TV);
		TextView tvAccount = (TextView) findViewById(GROUP_ACCOUNT_NAME_TV);
		ImageView iv = (ImageView) findViewById(GROUP_PIC_IB);
		
		String title = new String(!TextUtils.isEmpty(group.title)?group.title:"");
		String account = new String(!TextUtils.isEmpty(group.account.name)?group.account.name:"");
		int picResource = group.getIconResource();
		
		if (!TextUtils.isEmpty(title)) {
			title = title.replaceAll("(System|Group|:)", "")
					.trim();
		}
		tvName.setText(title);
		if (!TextUtils.isEmpty(account))
			tvAccount.setText(account);
		else {
			tvAccount.setText("phone");
		}
		int res = (picResource != Group.NO_ICON) ? picResource
				: Group.ICON_DEFAULT;
		iv.setImageResource(res);
	}

	@Override
	public void setStyle(Style style) {
	}

	@Override
	public void onClick(View view) {
		int id = view.getId();
		switch (id) {
		case GROUP_DELETE_IB:
			mController.onClickDelete(mInfo);
			break;
		case GROUP_PIC_IB:
			mController.onClickPic(mInfo);
			break;
		case GROUP_RINGTONE_IB:
			mController.onClickRingtone(mInfo);
			break;
		case GROUP_EDIT_IB:
			mController.onClickEdit(mInfo);
			break;
		case GROUP_LIST_IB:
			mController.onClickList(mInfo);
			break;
		}
	}
}
